<template>
  <el-input type="text" placeholder="请输入用户名" v-model="name"></el-input>
  <el-button @click="onSearch">查询搜索</el-button>
  <el-button @click="onAddUser">添加用户</el-button>
  <!-- 数据的展示 -->
  <el-table :data="tableData" style="width: 100%" :border="true">
    <el-table-column prop="id" label="编号" width="100" />
    <el-table-column prop="username" label="账号" width="200" />
    <el-table-column prop="nickName" label="姓名" width="180" />
    <el-table-column prop="email" label="邮箱" width="180" />
    <!-- 自定义列模板 -->
    <el-table-column label="添加时间" width="180">
      <!-- slot 插槽写法  scope就是作用域插槽 ， scope.row 就是数据 -->
      <template #default="scope">
        <div>
          {{
            scope.row.createTime
            ? dayjs(scope.row.createTime).format("YYYY-MM-DD HH:mm:ss")
            : "/"
          }}
        </div>
      </template>
    </el-table-column>
    <el-table-column label="最后登录" width="180">
      <!-- slot 插槽写法  scope就是作用域插槽 ， scope.row 就是数据 -->
      <template #default="scope">
        <div>
          {{
            scope.row.loginTime
            ? dayjs(scope.row.loginTime).format("YYYY-MM-DD HH:mm:ss")
            : ""
          }}
        </div>
      </template>
    </el-table-column>
    <el-table-column label="是否启用" width="100">
      <template #default="scope">
        <el-switch :value="scope.row.status === 1 ? true : false" />
      </template>
    </el-table-column>
    <el-table-column label="操作" width="180" flex="right">
      <template #default="scope">
        <el-button>分配角色</el-button>
        <el-button @click="onEdit(scope.row)">编辑</el-button>
      </template>
    </el-table-column>
  </el-table>

  <!-- 分页器 -->
  <el-pagination v-model:current-page="pagination.pageNum" v-model:page-size="pagination.pageSize" background
    :page-sizes="[1, 5, 10, 20]" layout="total, sizes, prev, pager, next, jumper" :total="pagination.total"
    @size-change="handleSizeChange" @current-change="handleCurrentChange" />

  <!-- 编辑组件 -->
  <Edit :visible="visible" :form="modalForm" @cancel="onCloseDialog" @confirm="onConfirmDialog"></Edit>
</template>

<script setup lang="ts">
import { ref } from "vue";
import type { Ref } from "vue";
import * as api from "../../json/request/api";
import useTable from "../../hooks/useTables";
import dayjs from "dayjs";
import Edit from "./Edit.vue";
import type { IAdminListItem } from "../../json/request/QmsAdminConreiller/index";
import { ElMessage, ElMessageBox } from "element-plus";

let name = ref("");
let add = false; // false编辑 true 新增
let modalForm = ref({}) as Ref<IAdminListItem>;

// keyword: name.value  // 可以删掉 name.value 不是一个响应式数据
let {
  tableData,
  getTableData,
  pagination,
  handleSizeChange,
  handleCurrentChange,
} = useTable(api.admin.getAdminList, {
  keyword: name, // name 是响应式的
});

// 弹框
let visible = ref(false);

// 添加用户
let onAddUser = () => {
  let newUser = {
    email: "",
    nickName: "",
    note: "",
    password: "",
    status: 0,
    username: "",
  } as IAdminListItem;
  modalForm.value = newUser;
  add = true;
  visible.value = true;
};
// 编辑
let onEdit = (row: IAdminListItem) => {
  // console.log(row);
  add = false;
  modalForm.value = row;
  // console.log(modalForm);
  visible.value = true;
};

// 子级emit组件传过来的事件
let onCloseDialog = () => {
  visible.value = false;
};

// 确认了输入框的内容之后
// 需要发送请求
let onConfirmDialog = (row: IAdminListItem) => {
  // console.log(row);  // row就是表单数据
  // 二次 确认
  ElMessageBox.confirm("是否确认提交!", "提示", {
    confirmButtonText: "OK",
    cancelButtonText: "Cancel",
    type: "warning",
  })
    .then(async () => {
      // 发请求
      // 去判断 是 编辑 还是添加
      if (add) {
        await api.admin.adminRegister(row); // 新增
      } else {
        await api.admin.adminUpdate(row); // 编辑
      }
      ElMessage({
        type: "success",
        message: "操作成功",
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消提交",
      });
    });
};

getTableData();
let onSearch = async () => {
  // console.log(name.value);
  getTableData();
  // 发起请求 ，搜索数据
  // let listData = await api.admin.getAdminList({ keyword: name.value })
};
</script>

<style scoped></style>
